package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.pojo.Tag;
import com.blog.pojo.Type;
import com.blog.service.TagService;
import com.blog.service.TypeService;
import com.blog.mapper.TypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author DELL
 * @description 针对表【t_type】的数据库操作Service实现
 * @createDate 2022-07-10 15:20:38
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type>
        implements TypeService {

    @Autowired
    private TagService tagService;

    @Override
    public Page<Type> page(int page, int pageSize, String typeContent) {
        Page<Type> typePage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Type> typeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        typeLambdaQueryWrapper.like(typeContent != null, Type::getTypeContent, typeContent);
        typeLambdaQueryWrapper.orderByDesc(Type::getUpdateTime);
        this.page(typePage, typeLambdaQueryWrapper);
        return this.page(typePage, typeLambdaQueryWrapper);
    }

    @Override
    @Transactional//多表操作,加上事务
    public boolean deleteType(Long typeId) {
        LambdaQueryWrapper<Tag> tagLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tagLambdaQueryWrapper.eq(Tag::getTypeId, typeId);
        List<Tag> tagList = tagService.list(tagLambdaQueryWrapper);
        if (tagList.size() == 0) {//该分类下无标签
            this.removeById(typeId);
            return true;
        } else {
            return false;
        }
    }
}




